package com.cheer.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cheer.common.Page;
import com.cheer.common.Result;
import com.cheer.dto.OaLearnClassAddDTO;
import com.cheer.dto.OaLearnClassUpdateDTO;
import com.cheer.entity.OaLearnClassEntity;
import com.cheer.enums.AdminRoleEnum;
import com.cheer.exception.ServiceException;
import com.cheer.param.OptLogShowParam;
import com.cheer.security.UserInfoHolder;
import com.cheer.service.OaAdminService;
import com.cheer.service.OaLearnClassService;
import com.cheer.service.OptLogService;
import com.cheer.vo.OaLearnClassInfoVO;
import com.cheer.vo.OaLearnClassListVO;
import com.cheer.vo.OptLogVO;
import com.cheer.vo.SimpleObjectVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;
import java.util.Objects;

/**
 * @author cheer
 */
@RestController
@RequestMapping("/learn/class")
@Api( tags = "机构控制")
public class OaLearnClassController {

    @Autowired
    private OaLearnClassService oaLearnClassService;

    @Autowired
    private OaAdminService oaAdminService;

    @Autowired
    private UserInfoHolder userInfoHolder;

    @Autowired
    private OptLogService optLogService;

    @ApiOperation("添加单位")
    @PostMapping("/add")
    public Result add(@RequestBody @Valid OaLearnClassAddDTO learnClassAddDTO) {
        if (learnClassAddDTO.getPid() == null) {
            learnClassAddDTO.setPid(userInfoHolder.getAdmin().getPid());
        }
        OaLearnClassEntity add = oaLearnClassService.add(learnClassAddDTO);
        optLogService.insertLog("新增","oa_learn_class", learnClassAddDTO.getName(), Long.valueOf(add.getId()));
        return Result.success();
    }

    @ApiOperation("查询下级单位")
    @GetMapping("/sub")
    public Result<List<SimpleObjectVO>> sub(@RequestParam Integer id) {
        return Result.success(oaLearnClassService.sub(id));
    }

    @ApiOperation("查询单位列表")
    @GetMapping("/list")
    public Result<Page<OaLearnClassListVO>> list(@RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                                 @RequestParam(required = false, defaultValue = "10") Integer pageSize,
                                                 @RequestParam(required = false) Integer id) {
        if (id == null) {
            id = userInfoHolder.getAdmin().getPid();
        }
        return Result.success(oaLearnClassService.list(pageNum, pageSize, id));
    }

    @ApiOperation("下载单位列表")
    @PostMapping("/download")
    public Result download(@RequestParam(required = false) Integer id, HttpServletResponse response) {
        if (id == null) {
            id = userInfoHolder.getAdmin().getPid();
        }
        oaLearnClassService.download(id, response);
        return Result.success();
    }


    @ApiOperation("删除单位")
    @GetMapping("/delete")
    @Transactional
    public Result delete(@RequestParam Integer id) {
        if (Objects.equals(id, userInfoHolder.getAdmin().getPid())){
            throw new ServiceException("不能删除自己的所属单位");
        }
        OaLearnClassEntity byId = oaLearnClassService.getById(id);
        oaLearnClassService.delete(id);
        // 删除单位管理员
        oaAdminService.deleteByPid(id);
        optLogService.insertLog("删除","oa_learn_class", byId.getName(), Long.valueOf(id));
        return Result.success();
    }

    @ApiOperation("查询单位详情")
    @GetMapping("/info")
    public Result<OaLearnClassInfoVO> info(@RequestParam Integer id) {
        OaLearnClassInfoVO info = oaLearnClassService.info(id);
        for (int i = 0; i < info.getPath().size(); i++) {
            if (Objects.equals(info.getPath().get(i), userInfoHolder.getAdmin().getPid())) {
                info.setPath(info.getPath().subList(i+1, info.getPath().size()));
                break;
            }
        }
        return Result.success(info);
    }

    @ApiOperation("修改单位")
    @PostMapping("/update")
    public Result update(@RequestBody @Valid OaLearnClassUpdateDTO learnClassUpdateDTO) {
        if (learnClassUpdateDTO.getPid() == null) {
            learnClassUpdateDTO.setPid(userInfoHolder.getAdmin().getPid());
        }
        // 是否是admin账号管理员
        if(userInfoHolder.getAdmin().getId() == 1){
            learnClassUpdateDTO.setIsAdminId(true);
        }else{
            learnClassUpdateDTO.setIsAdminId(false);
        }
        learnClassUpdateDTO.setIsAdmin(Objects.equals(userInfoHolder.getAdmin().getRoleId(), AdminRoleEnum.ADMIN.getCode()));
        oaLearnClassService.update(learnClassUpdateDTO);
        optLogService.insertLog("修改","oa_learn_class", learnClassUpdateDTO.getName(), Long.valueOf(learnClassUpdateDTO.getId()));
        return Result.success();
    }

    @ApiOperation("导入单位信息")
    @PostMapping("/import")
    public Result importLearnClass(Integer pid, MultipartFile file) {
        List<OaLearnClassEntity> oaLearnClassEntities = oaLearnClassService.importLearnClass(pid, file);
        optLogService.saveBatchLog("新增","oa_learn_class",oaLearnClassEntities);
        return Result.success();
    }

    @ApiOperation("导入单位信息")
    @PostMapping("/import/all")
    public Result importAll(MultipartFile file) {
        oaLearnClassService.importAll(file);
        return Result.success();
    }

    @ApiOperation("管理员操作机构日志")
    @PostMapping("/log")
    public Result log(@RequestBody OptLogShowParam optLogShow) {

        optLogShow.setTableName("oa_learn_class");
        IPage<OptLogVO> optLogVOIPage = optLogService.showLog(optLogShow);
        return Result.success(optLogVOIPage);
    }
}
